#include "StdAfx.h"
#include "DataMangr.h"
#include "CommUtil.h"

CDataMangr::CDataMangr(void)
{
	m_bMoreData = TRUE;
	m_pXmlDoc = NULL;
	m_pNodeData = NULL;
}


CDataMangr::~CDataMangr(void)
{
	if(m_pXmlDoc!=NULL){
		delete m_pXmlDoc;
		m_pXmlDoc = NULL;
	}
	DeleteCriticalSection(&m_pCriticalSection);
}

CDataMangr* CDataMangr::GetDataMangrInst()
{
	static CDataMangr* pInst = new CDataMangr();
	return pInst;
}

BOOL CDataMangr::Init(LPCTSTR lpCfgFile)
{
	m_pXmlDoc = new TiXmlDocument;
	BOOL bRtn = FALSE;
	do{
		if(!m_pXmlDoc->LoadFile(CommUtil::wstr2str(lpCfgFile).c_str())){
			break;
		}
		m_pNodeData = m_pXmlDoc->RootElement();
		if(NULL == m_pNodeData){
			break;
		}
		bRtn = TRUE;
		InitializeCriticalSection(&m_pCriticalSection);
	}while(0);
	
	return bRtn;
}

BOOL CDataMangr::HasMoreData()
{
	return m_bMoreData;
}

BOOL CDataMangr::NextData(VerifyData& data)
{
	EnterCriticalSection(&m_pCriticalSection);
	BOOL bRtn = FALSE;
	if(HasMoreData()){		
		static TiXmlNode* pPreNode = NULL;
		TiXmlNode* pCurrNode = NULL;
		if(NULL == pPreNode){
			pCurrNode = m_pNodeData->FirstChild();
		}
		else{
			pCurrNode = pPreNode->NextSibling();
		}

		if(NULL != pCurrNode){
			wcscpy_s(data.lpSrc, CommUtil::str2wstr(pCurrNode->ToElement()->Attribute("src")).c_str());
			wcscpy_s(data.lpDst, CommUtil::str2wstr(pCurrNode->ToElement()->Attribute("dst")).c_str());
			wcscpy_s(data.lpLocalData, CommUtil::str2wstr(pCurrNode->ToElement()->GetText()).c_str());
			data.bOverWrite = ( strcmp(pCurrNode->ToElement()->Attribute("overwrite"),"true")==0 );
			pPreNode = pCurrNode;
			m_bMoreData = (NULL != pCurrNode->NextSibling());
			bRtn = TRUE;
		}
		else{
			m_bMoreData = FALSE;
		}
	}
	LeaveCriticalSection(&m_pCriticalSection);

	return bRtn;
}
